﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace RedisChapter1
{
    public class Seckill
    {
        public static void Show(string id,int minute)
        {
            #region 自减1，返回自减后的值

            //开启10个线程去抢购
            Console.WriteLine($"在{minute}分0秒正式开始秒杀");
            var flag = true;

            while(flag)
            {
                if(DateTime.Now.Minute==minute)
                {
                    flag = false;
                    for(int i=0;i<10;i++)
                    {
                        string name = $"客户端{id}号：{i}";
                        Task.Run(()=> 
                        {
                            using var client = new RedisClient("127.0.0.1",6379,"Shris2020");
                            //从client 中取出number 并 -1
                            var num = client.Decr("number");
                            if (num < 0)
                            {
                                Console.WriteLine(name + "抢购失败");
                            }
                            else
                            {
                                Console.WriteLine(name+"***抢购成功！");
                            }
                        });
                        Thread.Sleep(10);


                    }
                }
            }


            #endregion
        }
    }
}
